package com.yanqu.road.server.cmd.systemgroup.newmonopolymkt.resgrab;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.CrossAppointCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketGroup;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;


@Cmd(code = Protocol.C_CROSS_NEW_MONOPOLY_MARKET_RES_GRAB_SEARCH, desc = "资源搜索")
public class CrossNewMonopolyMarketResGrabSearchCmd extends CrossAppointCommand {

    long userId;
    int resGroupId;
    int level;
    int startIndex;
    boolean isEmpty;
    int guanYin;
    UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        userId = packet.getPlayerId();
        NewMonopolyMarketProto.CrossNewMonopolyMarketResGrabSearchReqMsg crossReq = NewMonopolyMarketProto.CrossNewMonopolyMarketResGrabSearchReqMsg.parseFrom(packet.getBytes());
        NewMonopolyMarketProto.NewMonopolyMarketResGrabSearchReqMsg req = crossReq.getClientReq();
        resGroupId = req.getResGroupId();
        if(req.hasLevel()) {
            level = req.getLevel();
        }
        if(req.hasStartIndex()){
            startIndex = req.getStartIndex();
        }
        if(req.hasIsEmpty()) {
            isEmpty = req.getIsEmpty();
        }
        guanYin = crossReq.getGuanYin();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(crossReq.getUserInfo());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossNewMonopolyMarketGroup monopolyMarket = CrossNewMonopolyMarketMgr.getMarket(groupId);
        if(monopolyMarket == null){
            return;
        }
        CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        MessageHelper.sendPacket(serverId, userId , YanQuMessageUtils.buildMessage(Protocol.S_NEW_MONOPOLY_MARKET_RES_GRAB_SEARCH_FROM_CROSS,
                monopolyMarket.getMarketResRob().search(serverId, packet.getPlayerId(), resGroupId, level, guanYin, startIndex, isEmpty)));
    }

    @Override
    public void sendData() {

    }
}
